<template>
	<view >
		<view style=" display: flex;justify-content: center;
		background-color: #CFEAF1;height: 44px;width: 100%;align-items: center;
		position: sticky;top: 0;z-index: 1;">
			<image @click="nologin()" src="../../static/back.png" style="height: 30px;width: 30px;
		position: absolute;left: 10px;"></image>
			<h3>登录</h3>
		</view>

		<view class="container">

			<image src="../../static/logo.png" mode="" style="width: 120px;
		height: 120px;display: block;margin: 0 auto;margin-top: 50px;"></image>
			<uni-forms ref="form" :rules="{}" style="margin-top: 10px;">
				<uni-forms-item label="用户名" name="">
					<uni-easyinput v-model="users.username" type="text" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item label="密码" name="">
					<uni-easyinput v-model="users.password" type="password" placeholder="请输入密码" />
				</uni-forms-item>

			</uni-forms>
			<view class="" style="display: flex; margin-top: 20px;">
				<text style="flex: 3;text-align: center;" @click="reg()">注册</text>
				<text style="flex: 3;text-align: center;" @click="forget()">忘记密码</text>
			</view>

			<button v-show="logok" type="primary" style="margin-top: 50px;width: 80%;" @click="wulog()">登 录</button>
			<button v-show="!logok" type="primary" style="margin-top: 50px;width: 80%;" @click="log()">登 录</button>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				users: {},
				id: null,
				alluser: [],
				logok: true
			}
		},
		methods: {
			nologin() {
				uni.switchTab({
					url: '/pages/tab/my'
				})
			},
			reg() {
				uni.navigateTo({
					url: '../register/register'
				});
			},
			forget() {
				uni.navigateTo({
					url: '/pages/forget/forget'
				})
			},
			wulog() {
				uni.request({
					url: 'http://localhost:8484/ttwm/ttwm/loginUser',
					method: 'POST',
					data: this.users,
					success: res => {
						if (res.data.success) {

							this.id = res.data.data.userId
							console.log(this.id);

							uni.showToast({
								title: '成功',
								icon: 'none'
							});

							uni.setStorage({
								key: "userid",
								data: res.data.data

							})

							uni.switchTab({
								url: "/pages/tab/my"
							});
						} else {
							if (!this.users.username) {
								uni.showToast({
									title: '账号或密码不能为空',
									icon: 'none'
								})

							} else {
								if (this.users.username) {
									uni.showToast({
										title: '该账号不存在,请注册',
										icon: 'none'
									});

								}

							}
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			log() {
				uni.request({
					url: 'http://localhost:8484/ttwm/ttwm/loginUser',
					method: 'POST',
					data: this.users,
					success: res => {
						if (res.data.success) {

							this.id = res.data.data.userId
							console.log(this.id);

							uni.showToast({
								title: '成功',
								icon: 'none'
							});

							uni.setStorage({
								key: "userid",
								data: res.data.data

							})

							uni.switchTab({
								url: "/pages/tab/my"
							});
						} else {
							if (this.users.username != null) {

								if (!this.alluser) {
									uni.showToast({
										title: '该账号不存在,请注册',
										icon: 'none'
									});
								} else {
									for (var i = 0; i < this.alluser.length; i++) {
										if (!this.users.username) {
											uni.showToast({
												title: '账号或密码不能为空',
												icon: 'none'
											})
											break
										} else if (this.users.username != this.alluser[i].username) {
											uni.showToast({
												title: '该账号不存在,请注册',
												icon: 'none'
											});
										} else if (this.users.password != this.alluser[i].password) {
											uni.showToast({
												title: '密码错误，请重试',
												icon: 'none'
											});
											break
										}

									}
								}
							} else {
								uni.showToast({
									title: '账号或密码不能为空',
									icon: 'none'
								})
							}
						}
					},
					fail: () => {},
					complete: () => {}
				});



			}
		},
		onLoad() {
			uni.request({
				url: 'http://localhost:8484/ttwm/ttwm/allUser',
				method: 'POST',
				data: {},
				success: res => {
					if (res.data.success) {
						this.logok = false
						this.alluser = res.data.list
						// console.log(this.alluser);
					} else {
						this.logok = true
					}

				},
				fail: () => {},
				complete: () => {}
			});
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
		padding-top: 60px;

	}
</style>